package org.finalministry.database.dao;

import java.util.List;

import org.finalministry.objects.Territory;

public class TerritoryDAO extends AbstractDAO {

	private static final String TABLENAME = "territory";

	public TerritoryDAO() {
		super();
	}

	/**
	 * Insert a new Territory into the database.
	 * 
	 * @param Territory
	 * @return persisted ID
	 * @throws DataAccessLayerException
	 */
	public Territory merge(Territory territory) throws DataAccessLayerException {

		return (Territory) super.merge(territory);
	}

	/**
	 * Delete a detached Territory from the database.
	 * 
	 * @param Territory
	 */
	public void delete(Territory territory) throws DataAccessLayerException {

		super.delete(territory);
	}

	/**
	 * Find an Territory by its primary key.
	 * 
	 * @param id
	 * @return
	 */
	public Territory find(Long id) throws DataAccessLayerException {

		return (Territory) super.find(Territory.class, id);
	}

	/**
	 * Finds all Territorys in the database.
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Territory> findAll() throws DataAccessLayerException {

		return super.findAll(Territory.class);
	}

	/**
	 * Get table name
	 */
	@Override
	protected String getTableName() {

		return TABLENAME;
	}

	// public TerritoryDAO(IDatabaseConnection connection) {
	// super(connection);
	// }
	//
	// private Long lastGeneratedId() throws SQLException,
	// ClassNotFoundException {
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result = stmt.executeQuery("SELECT MAX(ID) FROM TERRITORY");
	//
	// if (result.next()) {
	// return result.getLong(1);
	// }
	// return null;
	// }
	//
	// /**
	// *
	// * @param territory
	// * @return
	// * @throws Exception
	// */
	// public Long create(Territory territory) throws Exception {
	//
	// String insert =
	// "INSERT INTO TERRITORY (CONGREGATIONID,NUMBER,CITY,POSTALCODE) VALUES(?,?,?,?)";
	//
	// if (territory.getArea() != null) {
	// insert =
	// "INSERT INTO TERRITORY (CONGREGATIONID,NUMBER,CITY,POSTALCODE,AREAID) VALUES(?,?,?,?,?)";
	// }
	//
	// PreparedStatement stmt =
	// getConnection().getConnection().prepareStatement(insert);
	//
	// stmt.setLong(1, territory.getCongregationID());
	// stmt.setLong(2, territory.getNumber());
	// stmt.setString(3, territory.getCity());
	// stmt.setString(4, territory.getPostalcode());
	//
	// if (territory.getArea() != null) {
	// stmt.setLong(5, territory.getArea().getId());
	// }
	//
	// stmt.executeUpdate();
	//
	// stmt.close();
	//
	// return lastGeneratedId();
	// }
	//
	// public void update(Territory territory) throws Exception {
	// String insert =
	// "UPDATE TERRITORY SET CONGREGATIONID = ?, NUMBER = ?, CITY = ?, POSTALCODE = ? WHERE ID = "
	// + territory.getId();
	//
	// if (territory.getArea() != null) {
	// insert =
	// "UPDATE TERRITORY SET CONGREGATIONID = ?, NUMBER = ?, CITY = ?, POSTALCODE = ?, AREAID = ? WHERE ID = "
	// + territory.getId();
	// }
	//
	// PreparedStatement stmt =
	// getConnection().getConnection().prepareStatement(insert);
	//
	// stmt.setLong(1, territory.getCongregationID());
	// stmt.setLong(2, territory.getNumber());
	// stmt.setString(3, territory.getCity());
	// stmt.setString(4, territory.getPostalcode());
	//
	// if (territory.getArea() != null) {
	// stmt.setLong(5, territory.getArea().getId());
	// }
	//
	// stmt.executeUpdate();
	//
	// stmt.close();
	// }
	//
	// public List<Territory> getAll() throws Exception {
	//
	// List<Territory> territoryList = new ArrayList<Territory>();
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result =
	// stmt.executeQuery("SELECT ID,CONGREGATIONID,NUMBER,CITY,POSTALCODE,AREAID FROM TERRITORY");
	//
	// while (result.isAfterLast() == false) {
	// Territory territory = extractTerritoryFromResult(result);
	//
	// if (territory == null) {
	// break;
	// }
	//
	// territoryList.add(territory);
	// }
	//
	// return territoryList;
	// }
	//
	// public List<Territory> getAllByCongregationId(Long congregationID) throws
	// Exception {
	//
	// List<Territory> territoryList = new ArrayList<Territory>();
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result =
	// stmt.executeQuery("SELECT ID,CONGREGATIONID,NUMBER,CITY,POSTALCODE,AREAID FROM TERRITORY WHERE CONGREGATIONID = "
	// + String.valueOf(congregationID));
	//
	// while (result.isAfterLast() == false) {
	// Territory territory = extractTerritoryFromResult(result);
	//
	// if (territory == null) {
	// break;
	// }
	//
	// territoryList.add(territory);
	// }
	//
	// return territoryList;
	// }
	//
	// public Territory getById(Long id) throws Exception {
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result =
	// stmt.executeQuery("SELECT ID,CONGREGATIONID,NUMBER,CITY,POSTALCODE,AREAID FROM TERRITORY WHERE ID = "
	// + id);
	//
	// return extractTerritoryFromResult(result);
	// }
	//
	// public void delete(Long id) throws Exception {
	// PreparedStatement stmt =
	// getConnection().getConnection().prepareStatement("DELETE TERRITORY WHERE ID = ?");
	//
	// stmt.setLong(1, id);
	//
	// stmt.executeUpdate();
	// }
	//
	// private Territory extractTerritoryFromResult(ResultSet result) throws
	// ParseException, SQLException, ClassNotFoundException {
	//
	// if (result.next()) {
	//
	// Territory territory = new Territory();
	// territory.setId(result.getLong(1));
	// territory.setCongregationID(result.getLong(2));
	// territory.setNumber(result.getLong(3));
	// territory.setCity(result.getString(4));
	// territory.setPostalcode(result.getString(5));
	//
	// if (result.getLong(6) > 0) {
	// Area area =
	// DaoFactory.getInstance().getAreaDAO().getById(result.getLong(6));
	// territory.setArea(area);
	// }
	//
	// return territory;
	// }
	//
	// return null;
	// }
}
